import axios from 'axios';
import store from '@/store'

const http = axios.create({
    // 设置超时时间
    timeout: 30000,
    baseURL: process.env.NODE_ENV === "development" ? '/api' : `${window.location.origin}/`,
    // 其他axios的配置详见：https://www.kancloud.cn/yunye/axios/234845

});

// 添加请求拦截器
http.interceptors.request.use(
    config => {
        config.headers['X-Access-Token'] = `${window.sessionStorage.getItem('gc_refresh_token')}`;
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

//响应拦截器
// 例子
// this.$接口名称(data2).then((result) => {
//     console.log(result);
// },err=>{
//     console.log(err)
//     console.log(123)
// })

http.interceptors.response.use(
    response => {
        store.state.loading = false;
        const res = response.data;
        return res
    },
    error => {
        return Promise.reject(error);
    }
);

export default http;
